% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

%%% @author CA Meijer
%%% @copyright 2012 CA Meijer
%%% @doc This module provides utility functions for adding and removing leading
%%%      and trailing slashes that will be incorporated into a URL.
%%% @end

-module(nimbula_utils).

%% Include files

%% Exported function
-export([add_trailing_slash/1,
		 remove_leading_slash/1,
		 remove_slashes/1,
		 remove_trailing_slash/1,
		 to_string/1]).

%% API
add_trailing_slash(Str) ->
	case lists:reverse(Str) of
		[$/|_Tail] ->
			Str;
		_ ->
			Str ++ "/"
	end.

remove_leading_slash(Str) ->
	case Str of
		[$/|Tail] ->
			Tail;
		_ ->
			Str
	end.

remove_slashes(Str) ->
	remove_leading_slash(remove_trailing_slash(Str)).

remove_trailing_slash(Str) ->
	case lists:reverse(Str) of
		[$/|Tail] ->
			lists:reverse(Tail);
		_ ->
			Str
	end.

to_string(Str) when is_list(Str) ->
	Str;
to_string(Bin) when is_binary(Bin) ->
	binary_to_list(Bin).

%% Internal functions


